﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;

namespace EZCRS.HCMS.Web.manage.ta
{
    public partial class ta_mod : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["iata"] != null)
                {
                    PageInit();
                }
            }
        }

        private void PageInit()
        {

            EZCRS.HCMS.Bll.AIR.TAChannel channel = new EZCRS.HCMS.Bll.AIR.TAChannel();

            List<EZCRS.HCMS.Entity.Channel> lstChannel = channel.GetAll();

            foreach (EZCRS.HCMS.Entity.Channel c in lstChannel)
            {
                ListItem li = new ListItem();

                li.Text = c.Name;
                li.Value = c.ID.ToString();

                cblChannel.Items.Add(li);
            }


            LoadData(Request.QueryString["iata"]);

        }


        private void LoadData(string IATA)
        {
            EZCRS.HCMS.Bll.baseinfo.HCRS_TA_BLL tabll = new EZCRS.HCMS.Bll.baseinfo.HCRS_TA_BLL();

            HCRS_TA ta = tabll.GetItem(IATA);

            txtName.Text = ta.Name;
            txtLinkman.Text = ta.Linkman;
            txtID.Text = ta.IATA;

            txtFax.Text = ta.Fax;
            txtTel.Text = ta.Tel;
            txtEmail.Text = ta.Email;
            txtRemark.Text = ta.Remark;

            List<HCRS_TA_Channel> lstChl = tabll.GetChannelByIATA(IATA);

            foreach (HCRS_TA_Channel cl in lstChl)
            {
                foreach (ListItem li in cblChannel.Items)
                {
                    if (li.Value == cl.Channel.ToString())
                    {
                        li.Selected = true;
                    }

                }
            }

        }

        protected void btnsave_Click(object sender, EventArgs e)
        {

            List<HCRS_TA_Channel> lstChannels = new List<HCRS_TA_Channel>();

            foreach (ListItem li in cblChannel.Items)
            {
                if (li.Selected)
                {
                    HCRS_TA_Channel cl = new HCRS_TA_Channel();
                    cl.Channel = int.Parse(li.Value);

                    lstChannels.Add(cl);

                }
            }

            if (lstChannels.Count == 0)
            {
                Response.Write(CFun.AlertMeg(Resources.Site.NoChannelSelected, "back", ""));

                return;
            }

            EZCRS.HCMS.Entity.HCRS_TA ta = new HCRS_TA();

            ta.Name = txtName.Text;
            ta.IATA = txtID.Text;
            ta.Linkman = txtLinkman.Text;
            ta.Tel = txtTel.Text;
            ta.Fax = txtFax.Text;
            ta.CrateDate = DateTime.Now;
            ta.Remark = txtRemark.Text;
            ta.Email = txtEmail.Text;

            EZCRS.HCMS.Bll.baseinfo.HCRS_TA_BLL tabll = new EZCRS.HCMS.Bll.baseinfo.HCRS_TA_BLL();

            if (!string.IsNullOrEmpty(ta.IATA))
            {
                bool bExists = tabll.IsExistsIATA(ta.IATA);

                if (bExists && ta.IATA != Request.QueryString["iata"])
                {
                    Response.Write(CFun.AlertMeg(Resources.Site.IATAExists, "back", ""));
                    return;
                }
            }

            if (string.IsNullOrEmpty(ta.Name.Trim()))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.NoName, "back", ""));

                return;
            }

            if (ta.Name.Length <= 4)
            {
                Response.Write(CFun.AlertMeg("名称字数太短", "back", ""));

                return;
            }

            if (string.IsNullOrEmpty(ta.Linkman))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.NoLinkman, "back", ""));
                return;
            }

            if (string.IsNullOrEmpty(ta.Tel))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.TelError, "back", ""));
                return;
            }

            bool b = tabll.Update(Request.QueryString["iata"], ta);

            if (b)
            {
                tabll.DeleteTAChannel(Request.QueryString["iata"]);

                b = tabll.Add_TA_Channel(lstChannels, ta);

            }

            Response.Write("<script>window.parent.Close()</script>");
        }

    }
}
